<template>
  <div>
    <div class="info_manage_header"><div class="span"><span>管理物品</span></div></div>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" >
      <el-form-item>
        <el-cascader
          v-model="searchInfo.catIdArray"
          :options="infoCat"
          :props="cascader_props"
          @change="handleInfoCat"
          class="cascader_type"
          >
        </el-cascader>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable style="width:200px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('admin:info:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button v-if="isAuth('admin:info:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="infoId"
        header-align="center"
        align="center"
        label="信息Id">
      </el-table-column>
      <el-table-column
        prop="catId"
        header-align="center"
        align="center"
        label="所属分类">
      </el-table-column>
      <el-table-column
        prop="infoTitle"
        header-align="center"
        align="center"
        label="标题">
      </el-table-column>
      <!-- prop="infoDefaultImg" -->
      <el-table-column
        
        header-align="center"
        align="center"
        label="默认图片">
        <template slot-scope="scope">
          <el-image v-if="scope.row.infoDefaultImg"
          style="width: 50px; height: 50px"
          :src="scope.row.infoDefaultImg"
          fit='cover'></el-image>
          <!-- <span style="margin-left: 10px">{{ scope.row.date }}</span> -->
        </template>
      </el-table-column>
      <!-- label="信息状态[0-待,1-审核,2-成功,-1-删,-2-失败]" -->
      <el-table-column
        prop="infoStatus"
        header-align="center"
        align="center"
        >
        <template slot="header" slot-scope="scope" >
          <el-select v-model="infostatusValue" placeholder="请选择" @change="infoStatusChange"
            style="width:100px">
            <el-option
              v-for="(item, index) in options"
              :key="index"
              :label="item.label"
              :value="item.value"
              >
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope">
          <span v-if="scope.row.infoStatus == 1">待审核</span>
          <span v-if="scope.row.infoStatus == 2">已发布</span>
          <span v-if="scope.row.infoStatus == -2">审核失败</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="上传者id">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.infoId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.infoId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
  </div>
</template>

<script>
  import AddOrUpdate from './info-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 5,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,

        infostatusValue: '',
        infoStatus: [-2,1,2],
        userId: ""+this.currentUser.user.uid,
        useUserId: '1',
        catId: '',

        infoCat:[],
        cascader_props:{
          value:'catId',
          label:'catName',
          expandTrigger: 'hover',
        },
        searchInfo:{
          catIdArray: [],
          catId: 0,
        },

        /*
          {
            "page":{
                "key":"威海",
                "limit": "6"
            },
            "infoStatus":[1,2,0,-1], //[0-待,1-审核,2-成功,-1-删,-2-失败]
            "userId": "1",
            "useUserId": "1",
            "catId": "5"
          }
         */
        options: [
          {
            value: 'all',
            label: '全部',
          },{
            value: 'status1',
            label: '待审核',
          },{
            value: 'status2',
            label: '已发布',
          },{
            value: 'status-2',
            label: '审核失败',
          }
        ]

      }
    },
    components: {
      AddOrUpdate
    },
    /*activated*/ 
    mounted () {
      this.getInfoCat()
      this.getDataList()

    },
    methods: {
      // 获取信息种类
      getInfoCat(){
          this.$http1({
              url: this.$http.adornUrl("/user/info/category/list/tree"),
              method: "get",
              // params: this.$http.adornParams({})
          }).then(({ data }) => {
              this.$message({
                  type: "success",
                  message: "成功获取到种类信息!",
              });
              this.infoCat = data.data;
          });
      },
      // 检测 信息种类的变化
      handleInfoCat(){
        let _catIdArray = this.searchInfo.catIdArray
        this.searchInfo.catId = _catIdArray[_catIdArray.length-1];
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        console.log("个人空间：获取info")
        this.dataListLoading = true
        var data = {
          page:{
            page:''+this.pageIndex,
            limit:''+this.pageSize,
            key: this.dataForm.key,
          },
          infoStatus: this.infoStatus,
          userId: this.userId,
          useUserId: this.useUserId,
          catId: ''+this.searchInfo.catId,
        }

        this.$http1({
          url: this.$http.adornUrl('/user/info/list'),
          // method: 'get',
          method: "post",
          // params: this.$http.adornParams({
          //   'page': this.pageIndex,
          //   'limit': this.pageSize,
          //   'key': this.dataForm.key
          // })
          data: this.$http.adornData(data, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.infoId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http1({
            url: this.$http.adornUrl('/admin/info/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 查询 信息种类发生修改
      infoStatusChange(val){

        if(val ==='all'){
          this.infoStatus = [ 1, 2, -2]
        }else if(val === 'status1'){
          this.infoStatus = [ 1 ]
        }else if(val === 'status2'){
          this.infoStatus = [ 2 ]
        }else if(val === 'status-2'){
          this.infoStatus = [ -2 ]
        }
        console.log("infoStatus发生修改", val, this.infoStatus);
        this.getDataList();
      }
    }
  }
</script>

<style>
.info_manage_header .span{
  font-size: 18px;
  margin: 10px 0;
}
</style>
